package com.p7.notepad.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.p7.notepad.R;
import com.p7.notepad.app.BaseActivity;
import com.p7.notepad.bean.NotepadDetails;
import com.p7.notepad.bean.NotepadInfo;
import com.p7.notepad.network.APIManager;
import com.p7.notepad.network.exception.ExceptionHandle;
import com.p7.notepad.network.listener.RequestListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class RecycleNotepadActivity extends BaseActivity {

    @BindView(R.id.recycle_notepad_title_page)
    TextView mTitlePage;
    @BindView(R.id.recycle_notepad_file_name)
    TextView mFileName;
    @BindView(R.id.recycle_notepad_title)
    TextView mTitle;
    @BindView(R.id.recycle_notepad_web)
    WebView mWeb;

    private String id;

    @Override
    public int getLayoutId() {
        return R.layout.activity_recycle_notepad;
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        Bundle bundle = getIntent().getBundleExtra("bundle");
        id = bundle.getString("id");
        initWebFile();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getNotepad();
    }

    private void initWebFile() {
        WebSettings webSettings = mWeb.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setTextZoom(260);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        mWeb.setInitialScale(100);
        mWeb.setWebViewClient(new WebViewClient());
    }

    private void getHtml(String text) {
        if (!TextUtils.isEmpty(text)) {
            String html = "";
            List<NotepadInfo> data = new Gson().fromJson(text, new TypeToken<List<NotepadInfo>>() {}.getType());
            for (NotepadInfo info : data) {
                if (!TextUtils.isEmpty(info.getInput())) {
                    html = html + "<p class=\"p\"><span style=\"font-family:微软雅黑;color:#2E2E2E;font-size:13.5000pt;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                            + info.getInput() + " </span></p>";
                }
                if (!TextUtils.isEmpty(info.getUrl())) {
                    html = html + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"" + info.getUrl() + "\" />";
                }
            }
            html =  "<html><body>" + html + "</body></html>";
            mWeb.loadDataWithBaseURL(null, html, "text/html","utf-8",null);
        }
    }

    @OnClick({R.id.recycle_notepad_quit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.recycle_notepad_quit:
                finish();
                break;
        }
    }

    private void getNotepad() {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("id", id);
        APIManager.onPost("notepad/getSingleNotepad", parameters, new RequestListener() {
            @Override
            public void onSuccess(String s) {
                Gson gson = new Gson();
                NotepadDetails notepad = gson.fromJson(s, NotepadDetails.class);
                if (notepad.getCode() == 1000) {
                    mTitle.setText(notepad.getData().getNotepad().getTitle());
                    getHtml(notepad.getData().getNotepad().getInfo());
                } else if (notepad.getCode() == 2001) {
                    startActivity(LoginActivity.class);
                } else {
                    setToast("网络加载失败，请稍后重试");
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable error) {
                setToast("网络加载失败，请稍后重试");
            }
        });
    }

    private void setToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
